/*
 * @(#)DbConfigModify.java   1.0  2021年11月24日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.auth.Login;
import com.swift.core.filter.annotation.NoSqlVal;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.data.DataModel;
import com.swift.core.service.SynInterface;
import com.swift.core.validator.DataValidator;
import com.swift.core.validator.annotation.ParamNotEmpty;
import com.swift.dts.dao.DbConfigDao;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.exception.ServiceException;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月24日
 */
@Service("dbConfigModify")
@NoSqlVal
@Login
public class DbConfigModify implements SynInterface {

    @Autowired
    private DbConfigDao dbConfigDao;

    /**
     * @see com.swift.core.service.SynInterface#doService(com.swift.core.model.ServiceRequest)
     */
    @Override
    @DataValidator(NotEmpty = { @ParamNotEmpty(param = "modify", message = "操作类型modify不能为空"),
        @ParamNotEmpty(param = "key", message = "连接描述key不能为空") })
    public DataModel doService(ServiceRequest req) {
        String modify = req.getData().getString("modify");
        if (modify.equalsIgnoreCase("create")) {
            insert(req);
            return null;
        }
        if (modify.equalsIgnoreCase("update")) {
            update(req);
            return null;
        }
        if (modify.equalsIgnoreCase("delete")) {
            delete(req);
            return null;
        }
        return null;
    }

    private void delete(ServiceRequest req) {
        String key = req.getData().getString("key");
        List<DtsDataBase> list = dbConfigDao.list();
        if(list==null) throw new ServiceException("删除的连接"+key+"不存在");
        for(int i=0;i<list.size();i++) {
            DtsDataBase old = list.get(i);
            if(old.getKey().equalsIgnoreCase(key)) {
                list.remove(i);
                dbConfigDao.save(list);
                break;
            }
        }
    }

    public void update(ServiceRequest req) {
        String key = req.getData().getString("key");
        List<DtsDataBase> list = dbConfigDao.list();
        if(list==null) throw new ServiceException("修改的连接"+key+"不存在");
        for(DtsDataBase old:list) {
            if(old.getKey().equalsIgnoreCase(key)) {
                setDtsDataBase(old,req.getData());
                dbConfigDao.save(list);
                break;
            }
        }
    }

    private void insert(ServiceRequest req) {
        String key = req.getData().getString("key");
        List<DtsDataBase> list = dbConfigDao.list();
        if (list == null) list = new LinkedList<DtsDataBase>();
        for (DtsDataBase old : list) {
            if (old.getKey().equalsIgnoreCase(key)) {
                throw new ServiceException(key + "已存在");
            }
        }
        list.add(setDtsDataBase(null,req.getData()));
        dbConfigDao.save(list);
    }

    private DtsDataBase setDtsDataBase(DtsDataBase old,DataModel data) {
        if(old==null) old = new DtsDataBase();
        old.setDriverClassName(data.getString("driverClassName"));
        old.setIsSource(data.getBoolean("isSource"));
        old.setIsRing(data.getBoolean("isRing"));
        old.setKey(data.getString("key"));
        old.setMaxPoolSize(data.getInteger("maxPoolSize"));
        old.setMinPoolSize(data.getInteger("minPoolSize"));
        old.setPassword(data.getString("password").split("\n"));
        old.setUrl(data.getString("url").split("\n"));
        old.setUserName(data.getString("userName").split("\n"));
        return old;
    }

}
